﻿using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using game.character.battle;
using game.character.weapon;
using lumo;
using lumo.display.object2D;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game.character.enemy
{

    /// <summary>
    /// The player.
    /// </summary>
    public class EnemyPufferfish : EnemyBase
    {

        protected const string _Texture = "Graphics/Enemies/Pufferfish";
        protected const int _Life = 1000;
        protected const uint _Hit = 1000;

        /// <summary>
        /// Constructor for RedEnemy.
        /// </summary>
        public EnemyPufferfish(LumoComponent component, GameMap map, Tiled.Object tiledObject)
            : base(component, map, tiledObject)
        {
            Battler = new GameEnemy(_Life);
            Battler.HitRate = _Hit;

            // Save the texture.
            Texture = component.Content.Load<Texture2D>(_Texture);
        }

        /// <summary>
        /// Update the character.
        /// </summary>
        /// <param name="component">Update using lumo</param>
        protected override void CreateBody()
        {
            // Create Submarine Body.
            Body = BodyFactory.CreateRectangle(Global.Map.World, Texture.Width / 4 / Global.PhysicsScale, Texture.Height / Global.PhysicsScale, 0);
            Body.BodyType = BodyType.Dynamic;
            Body.IsSensor = true;
            Body.IgnoreGravity = true;
            Body.CollisionCategories = Global.PhysicsCategory.Enemy;
            Body.CollidesWith = Global.PhysicsCategory.Actor | Global.PhysicsCategory.ActorBullet;
            Body.LinearDamping = 1.0f;
            Body.Restitution = 0.25f;
        }
        /// <summary>
        /// Setup parameters of the enemy.
        /// </summary>
        /// <param name="component">Update using lumo</param>
        protected override void SetupParameters()
        {
            // Create Submarine Body.
            Position = new Vector2(_TiledObject.X, _TiledObject.Y);
            SourceRectangle = new Rectangle(0, 0, Texture.Width / 4, Texture.Height);
            ScreenOrigin = new Vector2(Texture.Width / 8, Texture.Height / 2);

            //
            Weapon = new EnemyWeapon(_LumoComponent.Content.Load<Texture2D>("Graphics/Weapons/Bullet01"));
        }

        /// <summary>
        /// Update the character.
        /// </summary>
        /// <param name="component">Update using lumo</param>
        protected override void SetupAI()
        {
            //
            AIAction Timer = AIOnTimer(
                2000.0f,
                ()=>
                {
                    for (int y = -1; y < 2; y++)
                        for (int x = -1; x < 2; x++)
                        {
                            if ((x == 0) && (y == 0))
                                continue;
                            Weapon.Shoot(BulletManager, Position, Position + new Vector2(x, y), Vector2.Zero);
                        }
                    return "Shoot";
                }
            );
        }

    }

}